package com.flow.dao.impl;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.flow.dao.IBaseDao;
import com.flow.model.PagerModel;

/**
 * BaseDao
 * 
 * @author wang
 */
@SuppressWarnings("all")
public class BaseDao<T> extends HibernateDaoSupport implements IBaseDao<T> {

	/**
	 * 添加方法
	 */
	public void insert(Object object) {

		getHibernateTemplate().save(object);
	}

	/**
	 * 更新方法
	 */
	public void update(Object object) {

		getHibernateTemplate().update(object);
		
	}

	/**
	 * 删除放法
	 */
	public void delete(Object object) {

		getHibernateTemplate().delete(object);
		
	}

	/**
	 * 返回受影响行数
	 */
	public int universalMethod(String hql, List<Object> params) {
		
		Session session = getSession();
		int resultValue = 0;
		
		try {
			
			Query query = session.createQuery(hql);
			if (params != null && params.size() > 0) {
				for (int i = 0; i < params.size(); i++) {
					query.setParameter(i, params.get(i));
				}
			}
			resultValue = query.executeUpdate();
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}
		
		return resultValue;
	}

	/**
	 * 返回Ojbect对象
	 */
	public Object findByHql(String hql, List<Object> params) {
		
		Session session = getSession();
		Object o = null;
		
		try {
			
			Query query = session.createQuery(hql);
			if (params != null && params.size() > 0) {
				for (int i = 0; i < params.size(); i++) {
					query.setParameter(i, params.get(i));
				}
			}
			o = query.uniqueResult();
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}
		
		return o;
	}

	/**
	 * 返回ListObject
	 */
	public List<T> findAllByHql(String hql, List<Object> params) {

		Session session = getSession();
		List<T> list = null;
		try {
			
			Query query = session.createQuery(hql);
			if (params != null && params.size() > 0) {
				
				for (int i = 0; i < params.size(); i++) {
					
					query.setParameter(i, params.get(i));
					
				}
				
			}
			list = query.list();
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}
		
		return list;
	}
	
	/**
	 * 返回分页模型
	 */
	public PagerModel findPagerModeled(PagerModel pager, String hql, List<Object> params) {

		try {
			
			String aaaa = pager.getTotalCountHql(hql, params);
			Query query = getSession().createQuery(pager.getTotalCountHql(hql, params));
			
			if (params != null && params.size() > 0) {
				
				for (int i = 0; i < params.size(); i++) {
					
					query.setParameter(i, params.get(i));
					
				}
				
			}
			
			int total = ((Long) query.uniqueResult()).intValue();
			query = getSession().createQuery(hql);
			
			if (params != null && params.size() > 0) {
				
				for (int i = 0; i < params.size(); i++) {
					
					query.setParameter(i, params.get(i));
					
				}
				
			}
			query.setFirstResult(PagerModel.getOffset());
			query.setMaxResults(PagerModel.getPagesize());
			List datas = query.list();
			pager.setDatas(datas);
			pager.setTotal(total);
			
		} catch (Exception ex) {
			
			ex.printStackTrace();
			
		}
		
		return pager;
	}

}
